package com.zck.thumbup.lock;

import com.zck.thumbup.constant.RedisConstant;
import com.zck.thumbup.exception.BusinessException;
import com.zck.thumbup.exception.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Java + Jedis版本
 * @author 赵承康
 * @date 2025/10/17
 */
@Service
@RequiredArgsConstructor
public class RedissonSeckillService {
    private final RedissonClient redissonClient;
    private final StringRedisTemplate stringRedisTemplate;

    //点赞分布锁（Redisson锁实现）
    public void seckill(String blogId){
        //1. 定义锁key（按商品ID区分）
        String loclKey= RedisConstant.REDIS_LOCK_KEY +blogId;
        //2. 获取重入锁
        RLock lock = redissonClient.getLock(loclKey);
        try{
            //3. 获取锁（最多等待3秒，锁自动过期30秒，看门狗自动续期）
            boolean isLocked = lock.tryLock(3, 30, TimeUnit.SECONDS);
            if (!isLocked) {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR,"点赞失败：获取锁超时");
            }
            // 4. 执行业务（点赞博客）
        }catch (Exception e){

        }

    }


}
